package serverletGrade;

import Server.GradeServer;
import Server.StudentServer;
import user.Grade;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "motifyGradeServlet",urlPatterns = "/motifygrade")
public class motifyGradeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            Grade grade = null;
            GradeServer gradeServer = new GradeServer();
            String num = request.getParameter("num");
            grade = gradeServer.findGradeServer(num);
            System.out.println(grade);
            if(!"".equals(request.getParameter("name"))) {
                grade.setName(request.getParameter("name"));
            }
            if(!"".equals(request.getParameter("year"))) {
                grade.setYear(request.getParameter("year"));
            }
            if(!"".equals(request.getParameter("teacher"))) {
                grade.setTeacher(request.getParameter("teacher"));
            }
            if(!"".equals(request.getParameter("slogan"))) {
                grade.setSlogan(request.getParameter("slogan"));
            }
            if(!"".equals(request.getParameter("peopleNum"))) {
                grade.setPeopleNum(request.getParameter("peopleNum"));
            }
            System.out.println(grade);
            int i = gradeServer.motifyGradeServer(num,grade);
            System.out.println("修改"+i+"行");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/gradeinit");
            requestDispatcher.forward(request,response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
